﻿using Raffles.OA.Data;
using Raffles.OA.Utility;
using Mapster;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Raffles.OA.WebApp.Areas.Manage.Controllers
{
    public class DictionaryDetailController : BaseController<DictionaryDetail>
    {
        /// <summary>
        /// 获取字典数据分页列表
        /// </summary>
        /// <param name="param"></param>
        /// <param name="pagination"></param>
        /// <returns></returns>
        public ActionResult GetPageListJson(DetailSearchParam param, Pagination pagination)
        {
            int totalCount = 0;
            var details = _dbContext.Queryable<DictionaryDetail>().Where(x => !x.DeletedMark)
                                                    .WhereIF(!param.DicType.IsEmpty(), x => x.DicType == param.DicType.Trim())
                                                    .WhereIF(!param.KeyWord.IsEmpty(), x => x.DicCode == param.KeyWord.Trim()|| x.DicValue == param.KeyWord.Trim())
                                                    .OrderBy(x => x.DisplayOrder)
                                                    .ToPageList(pagination.PageIndex, pagination.PageSize, ref totalCount);
            return Content(DataResult<List<DictionaryDetail>>.PageResult(details, totalCount).ToJson());
        }

        public ActionResult Form(int? id)
        {
            DictionaryDetail detail = new DictionaryDetail();
            if (id != null)
            {
                detail = _dbContext.Queryable<DictionaryDetail>().InSingle(id);
            }
            else
            {

            }
            return View(detail);
        }

        /// <summary>
        /// 字典数据保存
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SaveForm(DictionaryDetailDto detailDto)
        {
            try
            {
                if (detailDto.Id == null)
                {
                    if (_dbContext.Queryable<DictionaryDetail>().Any(x => !x.DeletedMark && x.DicType == detailDto.DicType&&x.DicCode==detailDto.DicCode))
                    {
                        return Json(DataResult.Error("字典键不允许重复"));
                    }
                    DictionaryDetail detail = new DictionaryDetail();
                    detailDto.Adapt(detail);
                    detail.CreatedBy = OperatorProvider.Provider.GetCurrent().Id;
                    _dbContext.Insertable<DictionaryDetail>(detail).ExecuteCommand();
                    return Json(DataResult.Success("字典数据新增成功"));
                }
                else
                {
                    if (_dbContext.Queryable<DictionaryDetail>().Any(x => !x.DeletedMark && x.DicType == detailDto.DicType && x.DicCode == detailDto.DicCode && x.Id != detailDto.Id))
                    {
                        return Json(DataResult.Error("字典键不允许重复"));
                    }
                    DictionaryDetail detail = _dbContext.Queryable<DictionaryDetail>().InSingle(detailDto.Id);
                    detailDto.Adapt(detail);
                    detail.UpdatedOn = DateTime.Now;
                    detail.UpdatedBy = OperatorProvider.Provider.GetCurrent().Id;
                    _dbContext.Updateable<DictionaryDetail>(detail).ExecuteCommand();
                    return Json(DataResult.Success("字典典数据成功"));
                }
            }
            catch (Exception ex)
            {
                return Json(DataResult.Error($"保存失败,失败原因:{ex.Message}"));
            }
        }

        /// <summary>
        /// 字典数据删除
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DeleteData(int id)
        {
            DictionaryDetail detail = _dbContext.Queryable<DictionaryDetail>().InSingle(id);
            detail.DeletedMark = true;
            detail.UpdatedOn = DateTime.Now;
            detail.UpdatedBy = OperatorProvider.Provider.GetCurrent().Id;
            _dbContext.Updateable<DictionaryDetail>(detail).ExecuteCommand();
            return Json(DataResult.Success("删除成功"));
        }

        /// <summary>
        /// 状态改变
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ChangeStatus(int id)
        {
            DictionaryDetail detail = _dbContext.Queryable<DictionaryDetail>().InSingle(id);
            var result = detail.EnabledMark;
            detail.EnabledMark = !detail.EnabledMark;
            detail.UpdatedOn = DateTime.Now;
            detail.UpdatedBy = OperatorProvider.Provider.GetCurrent().Id;
            _dbContext.Updateable<DictionaryDetail>(detail).ExecuteCommand();
            return result == true ? Json(DataResult.Success("禁用成功")) : Json(DataResult.Success("启用成功"));
        }
    }
}